
#if MeshPro
using TMPro;
#endif
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// 显示MeshPro 面对摄像机进行显示
    /// </summary>
    public class HUDBase : MonoBehaviour
    {
#if MeshPro
        [HideInInspector]
        public long ownerID;
        public float scaleRate = 0.09f;
        public float scaleMin = 0f;
        public float scaleMax = 0.84f;
        public bool dontLookAtCamera;
        public bool dontScaleWithCamera;

        private Camera hudCamera;

        protected TextMeshPro nameText;

        
        public void Awake()
        {
            if (nameText == null)
            {
                nameText = GetComponent<TextMeshPro>();
            }
        }

        private void OnDestroy()
        {
            // HUDManagerInstance.Instance.RemoveHUD(ownerID);
        }



        public virtual void InstallBaseHUD(string name, int nameColor)
        {
            nameText = transform.Find("BattlePanel/NameMeshPro").GetComponent<TextMeshPro>();
            if (nameText != null)
            {
                nameText.text = name;
                // 名字颜色
                switch (nameColor)
                {
                    case 0:
                        {
                            nameText.color = HUDDefine.COLOR_HUDNAME00;
                            break;
                        }
                    case 1:
                        {
                            nameText.color = HUDDefine.COLOR_HUDNAME01;
                            break;
                        }
                    case 2:
                        {
                            nameText.color = HUDDefine.COLOR_HUDNAME02;
                            break;
                        }
                }
            }
        }

        /// <summary>
        /// 修改Title
        /// </summary>
        /// <param name="title"></param>
        /// <param name="guildname"></param>
        public void ChangeTitle(string title, string guildName, int nameColor, string guildIcon, int realmLevel, int guildPost)
        {
            OnTitleChanged(title, guildName, nameColor, guildIcon, realmLevel, guildPost);
        }

        public void UpdateBlood(int curHp, int maxHp, bool immediately = false)
        {
            if (maxHp == 0)
            {
                maxHp = 1;
            }

            OnUpdateBlood(curHp, maxHp, immediately);
        }

        protected virtual void OnUpdate()
        {

        }

        private void LateUpdate()
        {
            OnUpdate();

            if (hudCamera == null)
            {
                hudCamera = Camera.main;
                if (hudCamera == null)
                {
                    return;
                }
                // hudCamera = GameObject.FindWithTag("HUDCamera").GetComponent<Camera>();
            }

            if (!dontLookAtCamera)
            {
                transform.rotation = hudCamera.transform.rotation;
            }
            if (!dontScaleWithCamera)
            {
                float scale = Vector3.Distance(transform.position, hudCamera.transform.position) * scaleRate;
                transform.localScale = Vector3.one * Mathf.Clamp(scale, scaleMin, scaleMax);
            }
        }

        protected void SetColor(TextMeshPro textMeshPro, Color[] colors)
        {
            if (colors.Length == 1)
            {
                textMeshPro.enableVertexGradient = false;
                textMeshPro.color = colors[0];
            }
            else if (colors.Length == 2)
            {
                textMeshPro.enableVertexGradient = true;
                VertexGradient colorGradient = new VertexGradient();
                colorGradient.topLeft = colors[0];
                colorGradient.topRight = colors[0];
                colorGradient.bottomLeft = colors[1];
                colorGradient.bottomRight = colors[1];
                textMeshPro.colorGradient = colorGradient;
                textMeshPro.color = Color.white;
            }
            else if (colors.Length == 4)
            {
                textMeshPro.enableVertexGradient = true;
                VertexGradient colorGradient = new VertexGradient();
                colorGradient.topLeft = colors[0];
                colorGradient.topRight = colors[1];
                colorGradient.bottomLeft = colors[2];
                colorGradient.bottomRight = colors[3];
                textMeshPro.colorGradient = colorGradient;
                textMeshPro.color = Color.white;
            }
        }


        protected virtual void OnUpdateBlood(int curHp, int maxHp, bool immediately)
        {

        }

        protected virtual void OnTitleChanged(string title, string guildName, int nameColor, string guildIcon, int realmLevel, int guildPost)
        {

        }
#endif
    }

}